package edu.rjxy.xjdx.emps.dao;

import edu.rjxy.xjdx.emps.common.domain.User;
import edu.rjxy.xjdx.emps.common.utils.MyDbUtil;

import java.sql.*;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public User selectUserByLoginName(String loginName) {
        //列名 和 实体类的属性名一致,如果不一致，查询时候通过别名来处理
        String sql =" select id,loginName,password ,lastLoginTime from user where loginName = ? ";
        List<User> userList = MyDbUtil.executeQuery(User.class,sql ,loginName);
        return (userList.size()>0?userList.get(0):null);
    }

    @Override
    public void updatePasswordByLoginname(String newpassword, String loginName) {
        String sql =" update  user set password = ? where loginName = ? ";
        MyDbUtil.executeUpdate(sql,newpassword,loginName);
    }


}


//JDBC
  /*      try {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://127.0.0.1:3306/mydb";
                Connection connection = DriverManager.getConnection(url,"root","root");
                PreparedStatement statement = connection.prepareStatement(sql);
                statement.setString(1,loginName.trim());
                ResultSet resultSet = statement.executeQuery();
                if(resultSet.next()){
                int id = resultSet.getInt("id");
                String loginNameInDB = resultSet.getString("loginName");
                String passwordInDB = resultSet.getString("password");
                Date lastLoginTime = resultSet.getDate("lastLoginTime");
                user=new User(id,loginNameInDB,passwordInDB,lastLoginTime);
                }
                } catch (Exception e) {
                e.printStackTrace();
                }*/